package com.thread.laoma;

/**
 * Created by zhuangqi on 17-4-7.
 */
public class FireRun {
    static class FireFlag{
        private volatile boolean fired = false;

        public synchronized void waitForFire() throws InterruptedException {
            while (!fired){
                wait();
            }
        }
        public synchronized void fire(){
            this.fired = true;
            notifyAll();
        }
    }

    static class Racer extends Thread{
        FireFlag fireFlag;

        public Racer(FireFlag fireFlag) {
            this.fireFlag = fireFlag;
        }

        @Override
        public void run() {
            try {
                this.fireFlag.waitForFire();
                System.out.println("start run :" + Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        public static void main(String[] args) throws InterruptedException {
            FireFlag fireFlag = new FireFlag();
            Thread[] racers = new Thread[10];
            for (int i = 0; i < 10; i++) {
                racers[i] = new Racer(fireFlag);
                racers[i].start();
            }
            Thread.sleep(1000);
            fireFlag.fire();
        }
    }
}
